package com.itcast.donkey.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.adapters.ConSumeNotesAdapter;
import com.itcast.donkey.application.MyApplication;
import com.itcast.donkey.modles.DbAccountModles;
import com.itcast.donkey.modles.ScroListView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/9/6.
 */
public class ToolTravelActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout linScenicSpot;
    private ImageView backScenicApot;
    private ImageView ivChanScenicSpot;
    private TextView tvTitleScenicSpot;
    private ImageView ivToolTravel;
    private TextView tvMoney;
    private LinearLayout linClickToolTravel;
    private ScroListView scroListView;
    private TextView tv_custom_notes;

    private String currency_display;

    private SharedPreferences sp;

    private List<DbAccountModles> resultModles = new ArrayList<>();

    private MyApplication application = new MyApplication();
    private DbManager db;

    private ConSumeNotesAdapter adapter;
    //记录旅行中的所有花费
    private double money;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_travel);
        sp = getSharedPreferences("config", MODE_PRIVATE);

        initView();
        initData();
        initEvent();
    }

    private void initView() {

        linScenicSpot = (LinearLayout) findViewById(R.id.lin_scenic_spot);
        backScenicApot = (ImageView) findViewById(R.id.back_scenic_apot);
        ivChanScenicSpot = (ImageView) findViewById(R.id.iv_chan_scenic_spot);
        tvTitleScenicSpot = (TextView) findViewById(R.id.tv_title_scenic_spot);
        ivToolTravel = (ImageView) findViewById(R.id.iv_tool_travel);
        tvMoney = (TextView) findViewById(R.id.tv_money);
        linClickToolTravel = (LinearLayout) findViewById(R.id.lin_click_tool_travel);
        scroListView = (ScroListView) findViewById(R.id.scro_listview);

        tv_custom_notes = (TextView) findViewById(R.id.tv_custon_notes);

    }

    private void initData() {

        Intent intent = getIntent();
        currency_display = intent.getStringExtra("currency_display");
        tvMoney.setText("共0.0" + currency_display);

        int id = sp.getInt("id", 0);

        db = x.getDb(application.getConfig());
        try {
            List<DbAccountModles> modlesList = db.selector(DbAccountModles.class).findAll();
            if (modlesList.size() > 0) {
                for (int i = 0; i < modlesList.size(); i++) {
                    if (modlesList.get(i).getBreadId() == id) {
                        tv_custom_notes.setVisibility(View.GONE);
                        DbAccountModles modles = modlesList.get(i);
                        int length = modles.getPrice().length() - currency_display.length();
                        String resultMoney = modles.getPrice().substring(0, length);
                        double a = Double.valueOf(resultMoney);
                        money += a;
                        tvMoney.setText("共" + money + currency_display);
                        resultModles.add(modles);
                    }
                }
            }
            if (resultModles.size() > 0) {
                adapter = new ConSumeNotesAdapter(ToolTravelActivity.this, resultModles);
                scroListView.setAdapter(adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void initEvent() {

        backScenicApot.setOnClickListener(this);
        ivChanScenicSpot.setOnClickListener(this);
        ivToolTravel.setOnClickListener(this);
        linClickToolTravel.setOnClickListener(this);


        scroListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                /**
                 * 消费记录的弹出框
                 */
                AlertDialog.Builder builder = new AlertDialog.Builder(ToolTravelActivity.this);
                View view1 = View.inflate(ToolTravelActivity.this, R.layout.dialog_travel_notes, null);
                builder.setView(view1);
                builder.setCancelable(false);
                final AlertDialog ad = builder.create();
                TextView btn_cancel = (TextView) view1.findViewById(R.id.btn_cancel);
                TextView btn_certain = (TextView) view1.findViewById(R.id.btn_certain);
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ad.cancel();
                    }
                });
                btn_certain.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            DbAccountModles modles = db.selector(DbAccountModles.class).where("content", "=", resultModles.get(position).getContent()).findFirst();
                            db.delete(modles);
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                        Intent intent = new Intent(ToolTravelActivity.this, ToolTravelActivity.class);
                        intent.putExtra("currency_display", currency_display);
                        startActivity(intent);
                        finish();
                        ad.cancel();
                    }
                });


                ad.show();


            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scenic_apot:
            case R.id.iv_chan_scenic_spot:
                finish();
                break;
            case R.id.iv_tool_travel:
                Intent intent = new Intent(ToolTravelActivity.this, ConsumeNotesActivity.class);
                intent.putExtra("currency_display", currency_display);
                startActivity(intent);
                finish();
                break;
            case R.id.lin_click_tool_travel:
                Intent intent1 = new Intent(ToolTravelActivity.this, ConsumNoteDetailActivity.class);
                intent1.putExtra("currency_display", currency_display);
                intent1.putExtra("modles", (Serializable) resultModles);
                startActivity(intent1);
                break;


        }
    }
}
